#!/bin/bash
#################################################
#         Name: init.sh
#       Author: Lokion
#         Mail: lokion@qq.com
#     Function: 构建lnmp环境 && 部署项目
#################################################
# 必须要要脚本所在目录运行该脚本以防出错

#optimization linux system
. /etc/init.d/functions
PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/bin
export PATH

## 找到lnmp-compose项目目录
LNMP_DIR=`find / -type d -name lnmp-compose`
echo ${LNMP_DIR}
# 拷贝环境配置文件使其生效
source ${LNMP_DIR}/env.sample
cp -f ${LNMP_DIR}/env.sample ${LNMP_DIR}/.env
echo ${REDIS_PASSWORD}

#update yum info
function update_yum(){
  BASE_REPO=/etc/yum.repos.d/CentOS-Base.repo
  EPEL_REPO=/etc/yum.repos.d/epel.repo
  # 如果没有改成阿里源就修改为阿里源
  grep "mirrors.aliyun.com" ${BASE_REPO} >/dev/null 2>&1
  if [ $? -ne 0 ]; then
    cp ${BASE_REPO}{,.`date +%F_%H%M%S`} &&\
    yum -y install wget >/dev/null 2>&1 &&\
    wget -q -O ${BASE_REPO} http://mirrors.aliyun.com/repo/Centos-7.repo
  fi
  # 如果没有改成阿里源就修改为阿里源
  grep "mirrors.aliyun.com" ${EPEL_REPO} >/dev/null 2>&1
  if [ $? -ne 0 ]; then
    cp ${EPEL_REPO}{,.`date +%F_%H%M%S`} &&\
    yum -y install wget >/dev/null 2>&1 &&\
    wget -q -O ${EPEL_REPO} http://mirrors.aliyun.com/repo/epel-7.repo
  fi
  #yum clean all >/dev/null 2>&1
  #yum makecache >/dev/null 2>&1
  #yum upgrade >/dev/null 2>&1

  # 安装脚本将要用到的工具
  yum -y install wget git unzip curl >/dev/null 2>&1
  # 升级系统包
  yum -y update >/dev/null 2>&1
  # 安装常用工具
  yum -y install vim lsof lrzsz tree net-tools nmap bash-completion dos2unix nc telnet ntp rng-tools psmisc screen htop iftop iotop >/dev/null 2>&1

  action "update_yum" /bin/true
}


## 服务器ssh配置
function ssh_optimize(){
  # 需要修改的配置文件位置
  SSHD_CNF=/etc/ssh/sshd_config

  # SSH_CNF=/etc/ssh/ssh_config
  # # 解决Password authentication failed. Please verify that the usename and password are correct
  # sed -i "s/#   PasswordAuthentication yes/   PasswordAuthentication yes/" ${SSH_CNF}
  # # 确认修改结果
  # grep PasswordAuthentication ${SSH_CNF}

  # 启用客户端活动检查，每60秒检查一次，3次不活动断开连接
  sed -i "s/#ClientAliveInterval 0/ClientAliveInterval 60/" ${SSHD_CNF}
  sed -i "s/#ClientAliveCountMax 3/ClientAliveCountMax 3/" ${SSHD_CNF}
  # 启用密码验证
  sed -n "s/.*PasswordAuthentication .*/PasswordAuthentication yes/p" ${SSHD_CNF}
  # 确认修改结果
  grep -E "ClientAlive|PasswordAuthentication" ${SSHD_CNF}

  # 重新加载ssh配置，让配置生效
  systemctl reload sshd
  systemctl restart sshd

  action "ssh_optimize" /bin/true
}


## 服务器基础环境配置
function sys_optimize(){
  #	关闭防火墙
  systemctl stop firewalld.service
  systemctl disable firewalld.service
  systemctl status firewalld.service

  # 关闭selinux
  getenforce
  setenforce 0
  getenforce
  sed -i s'/^SELINUX=.*/SELINUX=disabled/' /etc/sysconfig/selinux
  grep ^SELINUX= /etc/sysconfig/selinux

  # 设置history命令时间格式
  PRO_FILE=/etc/profile
  # 如果没有修改过就修改
  grep "HISTIMEFORMAT=" ${PRO_FILE} >/dev/null 2>&1
  if [ $? -ne 0 ]; then
    echo "" >>${PRO_FILE}
    echo "# Set history format by terry" >>${PRO_FILE}
    echo 'export HISTIMEFORMAT="$IP `whoami` %Y-%m-%d %H:%M:%S"' >>${PRO_FILE}
    source ${PRO_FILE}
  fi

  action "sys_optimize" /bin/true
}

## docker安装
function docker_install(){
  # 判断docker是否已经安装，没有才安装
  if [ -f "/usr/bin/docker" ];then
    echo "系统已经安装docker！"
  else
    # 安装docker及devicemapper存储驱动需要device-mapper-persistent-data和lvm2支持
    # 安装yum-utils，它提供yum-config-manager程序
    yum -y install yum-utils device-mapper-persistent-data lvm2 >/dev/null 2>&1
    # 添加软件源信息
    yum-config-manager --add-repo http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
    # 默认安装最新版docker，要求安装docker 17以上的版本
    yum -y install docker-ce >/dev/null 2>&1
    # 查看docker可安装版本
    #yum list docker-ce.x86_64  --showduplicates |sort -r

    # 配置阿里云镜像加速
    mkdir -p /etc/docker
    cat > /etc/docker/daemon.json < 'EOF'
    {
      "registry-mirrors": ["https://prkehw25.mirror.aliyuncs.com"]
    }
    EOF
    # # 配置Docker-cn镜像加速
    # mkdir -p /etc/docker
    # tee /etc/docker/daemon.json <<-'EOF'
    # {
    #   "registry-mirrors": ["https://registry.docker-cn.com"]
    # }
    # EOF

    # 重新加载
    systemctl daemon-reload
    # 开机启动
    systemctl enable docker
    # 重起服务
    systemctl restart docker
  fi
  # 安装确认
  docker --version

  action "docker_install" /bin/true
}


## docker-compose 安装
function docker_compose_install(){
  # 判断docker-compose是否已经安装，没有才安装
  if [ -f "/usr/local/bin/docker-compose" ];then
    echo "系统已经安装docker-compose！"
  else
    # 本地安装
    if [ -f "docker-ext/docker-compose-Linux-x86_64" ];then
      cp -f "docker-ext/docker-compose-Linux-x86_64" "/usr/local/bin/docker-compose"
    else
      # 本地失败，使用在线安装docker-compose
      curl -L "https://github.com/docker/compose/releases/download/1.23.2/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose
    fi
    # 设置执行权限
    chmod +x /usr/local/bin/docker-compose
    # 设置软连接
    ln -s /usr/local/bin/docker-compose /usr/bin/docker-compose
  fi
  # 安装确认
  docker-compose --version

  action "docker_compose_install" /bin/true
}


## lnmp-compose环境配置修改
function set_lnmp(){
    # 需要修改的配置文件位置
    REDIS_CNF=${LNMP_DIR}/conf/redis.conf

    # # 拷贝环境配置文件使其生效
    # cp -f ${LNMP_DIR}/env.sample ${LNMP_DIR}/.env

    # 修改redis密码
    sed -i "s/# requirepass yourpassword/requirepass ${REDIS_PASSWORD}/" ${REDIS_CNF}
    grep requirepass ${REDIS_CNF}

    # # 复制mysql配置文件到指定目录
    # mkdir -p ${MYSQL_CONF_DIR} && mkdir -p ${MYSQL_DATA_DIR}
    # cp -f ${LNMP_DIR}/conf/mysql.cnf ${MYSQL_CONF_FILE}
    # # 复制redis配置文件到指定目录
    # mkdir -p ${REDIS_CONF_FILE} && mkdir -p ${REDIS_DATA_DIR}
    # cp -f ${LNMP_DIR}/conf/redis.conf ${REDIS_CONF_FILE}
    # # 复制php配置文件到指定目录
    # mkdir -p ${PHP72_PHP_CONF_DIR}
    # cp -f ${LNMP_DIR}/conf/php.ini ${PHP72_PHP_CONF_FILE}
    # cp -f ${LNMP_DIR}/conf/php-fpm.conf ${PHP72_FPM_CONF_FILE}
    # # 复制nginx配置文件到指定目录
    # mkdir -p ${NGINX_CONFD_DIR}
    # cp -f ${LNMP_DIR}/conf/nginx.conf ${NGINX_CONF_FILE}
    # \cp -fr ${LNMP_DIR}/conf/conf.d/* ${NGINX_CONFD_DIR}

    # 进入lnmp-compose项目目录
    cd ${LNMP_DIR}
    # 启动
    docker-compose up -d
    # build images
    #docker-compose build
}


## Git四方项目
function git_pay(){
  # 建立工作目录
  mkdir -pv /server
  cd /server
  # # 拷贝环境配置文件使其生效
  # cp -f ${LNMP_DIR}/env.sample ${LNMP_DIR}/.env

  # 清除目录
  rm -fr ${GIT_NAME}
  # git四方项目代码
  git clone -b ${GIT_BRANCH} "http://"${GIT_USERNAME}":"${GIT_PASSWORD}"@"${GIT_URL}
  #git clone -b master http://longanhou:Qwer1234@129.211.11.3/xiao/pay.git

  # 项目文件复制到nginx目录下
  \cp -r ${GIT_NAME}/* ${NGINX_HTML_DIR}/
  # 设置权限
  #chmod -R 644 ${NGINX_HTML_DIR}/
  chmod -R 777 ${NGINX_HTML_DIR}/Runtime/
  chmod -R 777 ${NGINX_HTML_DIR}/Uploads/
  chmod -R 777 ${NGINX_HTML_DIR}/Application/Common/Conf/

  # 部署项目有安装向导，如果要手动部署修改如下三个文件
  # ${NGINX_HTML_DIR}/Application/Common/Conf/db.php #配置数据库连接
  # ${NGINX_HTML_DIR}/Application/Common/Conf/website.php #DOMAIN必为网站域名
  # ${NGINX_HTML_DIR}/Application/Common/Conf/install.lock #新增此文件内容为lock
}


function main(){
    ssh_optimize
    sys_optimize
    update_yum
    docker_install
    docker_compose_install
    set_lnmp
    git_pay
}

main
action "项目部署完成！" /bin/true
